package com.coffee_shop_app.database;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.coffee_shop_app.database.dao.PcrData;
import com.coffee_shop_app.database.sqlite.DatabaseHelper;

import java.util.ArrayList;
import java.util.List;

public class PcrDataDatabase {
    private final DatabaseHelper dbHelper;

    public PcrDataDatabase(Context context) {
        super();
        dbHelper = new DatabaseHelper(context);
    }

    /**
     * 增
     *
     * @param data
     */
    public void insert(PcrData data) {
        String sql = "insert into " + DatabaseHelper.CONTACTS_TABLE_NAME;
        sql += "(username, serial_no, link, status, ip, create_time, channel, altitude, device_no, kit_no, temperature, humidity, pressure, longitude, latitude, fluid_ver, result, start_time, host_fw_ver, host_hw_ver, client_hw_ver, client_fw_ver, pump_fw_ver, valve_fw_ver, chipname_c_n, chipname_e_n, custom, host_fw_type, result_show, type, remark, sample_no) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        SQLiteDatabase sqlite = dbHelper.getWritableDatabase();
        sqlite.execSQL(sql, new String[] {data.getUsername() + "", data.getSerialNo() + "", data.getLink() + "", data.getStatus() + "", data.getIp() + "", data.getCreateTime() + "", data.getChannel() + "", data.getAltitude() + "", data.getDeviceNo() + "", data.getKitNo() + "", data.getTemperature() + "", data.getHumidity() + "", data.getPressure() + "", data.getLongitude() + "", data.getLatitude() + "", data.getFluidVer() + "", data.getResult() + "", data.getStartTime() + "", data.getHostFwVer() + "", data.getHostHwVer() + "", data.getClientHwVer() + "", data.getClientFwVer() + "", data.getPumpFwVer() + "", data.getValveFwVer() + "", data.getChipnameCN() + "", data.getChipnameEN() + "", data.getCustom() + "", data.getHostFwType() + "", data.getResultShow() + "", data.getType() + "", data.getRemark() + "", data.getSampleNo() + ""});
        sqlite.close();

    }

    /**
     * 删
     *
     * @param where
     */
    public void delete(String where) {
        SQLiteDatabase sqlite = dbHelper.getWritableDatabase();
        String sql = "delete from " + DatabaseHelper.CONTACTS_TABLE_NAME + where;
        sqlite.execSQL(sql);
        sqlite.close();
    }

//    /**
//     * 改
//     *
//     * @param data
//     */
//    public void update(PcrData data) {
//        SQLiteDatabase sqlite = dbHelper.getWritableDatabase();
//        String sql = ("update " + DatabaseHelper.CONTACTS_TABLE_NAME +" set sendPhone=?,name=?,phone=? where cid=?");
//        sqlite.execSQL(sql,
//                new String[] { data.getSendPhone() + "", data.getName() + "", data.getPhone()+ "", data.getCid()+ ""});
//        sqlite.close();
//    }
    /**
     * 查一条数据
     *
     * @param where
     * @return
     */
    public PcrData query(String where) {
        PcrData pcrData = null;
        SQLiteDatabase sqlite = dbHelper.getReadableDatabase();
        String sql= "select * from "
                + DatabaseHelper.CONTACTS_TABLE_NAME + where;
        Cursor cursor = sqlite.rawQuery(sql, null);
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            pcrData = setPcrdataValue(cursor);
        }
        if (!cursor.isClosed()) {
            cursor.close();
        }
        sqlite.close();
        return pcrData;
    }

    /**
     * 查
     *
     * @return
     */
    public List<PcrData> queryList(String where) {
        SQLiteDatabase sqlite = dbHelper.getReadableDatabase();
        ArrayList<PcrData> data = null;
        data = new ArrayList<>();
        String sql="select * from "
                + DatabaseHelper.CONTACTS_TABLE_NAME + where
                + " order by id desc ";
        Cursor cursor = sqlite.rawQuery(sql, null);
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            data.add(setPcrdataValue(cursor));
        }
        if (!cursor.isClosed()) {
            cursor.close();
        }
        sqlite.close();
        return data;
    }
    /**
     * 查
     *
     * @param where
     * @return
     */
    public int queryCount(String where) {
        SQLiteDatabase sqlite = dbHelper.getReadableDatabase();
        String sql="select count(*) from "
                + DatabaseHelper.CONTACTS_TABLE_NAME+where ;
        Cursor cursor = sqlite.rawQuery(sql, null);
        cursor.moveToFirst();
        int count = cursor.getInt(0);
        sqlite.close();
        return count;
    }

    /**
     * 重置
     *
     * @param datas
     */
    public void reset(List<PcrData> datas) {
        if (datas != null) {
            SQLiteDatabase sqlite = dbHelper.getWritableDatabase();
            // 删除全部
            sqlite.execSQL("delete from " + DatabaseHelper.CONTACTS_TABLE_NAME);
            // 重新添加
            for (PcrData data : datas) {
                insert(data);
            }
            sqlite.close();
        }
    }

    /**
     * 关闭连接
     */
    public void destroy() {
        dbHelper.close();
    }

    /**
     *  设置pcrdata值
     * @param cursor
     * @return
     */
    private PcrData setPcrdataValue(Cursor cursor){
        PcrData pcrData = new PcrData();
        pcrData.setId(cursor.getInt(0));
        pcrData.setUsername(cursor.getString(0 + 1));
        pcrData.setSerialNo(cursor.getString(1 + 1));
        pcrData.setLink(cursor.getString(2 + 1));
        pcrData.setStatus(cursor.getInt(3 + 1));
        pcrData.setIp(cursor.getString(4 + 1));
        pcrData.setCreateTime(cursor.getString(5 + 1));
        pcrData.setChannel(cursor.getString(6 + 1));
        pcrData.setAltitude(cursor.getFloat(7 + 1));
        pcrData.setDeviceNo(cursor.getString(8 + 1));
        pcrData.setKitNo(cursor.getString(9 + 1));
        pcrData.setTemperature(cursor.getFloat(10 + 1));
        pcrData.setHumidity(cursor.getFloat(11 + 1));
        pcrData.setPressure(cursor.getFloat(12 + 1));
        pcrData.setLongitude(cursor.getString(13 + 1));
        pcrData.setLatitude(cursor.getString(14 + 1));
        pcrData.setFluidVer(cursor.getString(15 + 1));
        pcrData.setResult(cursor.getString(16 + 1));
        pcrData.setStartTime(cursor.getLong(17 + 1));
        pcrData.setHostFwVer(cursor.getString(18 + 1));
        pcrData.setHostHwVer(cursor.getString(19 + 1));
        pcrData.setClientHwVer(cursor.getString(20 + 1));
        pcrData.setClientFwVer(cursor.getString(21 + 1));
        pcrData.setPumpFwVer(cursor.getString(22 + 1));
        pcrData.setValveFwVer(cursor.getString(23 + 1));
        pcrData.setChipnameCN(cursor.getString(24 + 1));
        pcrData.setChipnameEN(cursor.getString(25 + 1));
        pcrData.setCustom(cursor.getString(26 + 1));
        pcrData.setHostFwType(cursor.getString(27 + 1));
        pcrData.setResultShow(cursor.getString(28 + 1));
        pcrData.setType(cursor.getString(29 + 1));
        pcrData.setRemark(cursor.getString(30 + 1));
        pcrData.setSampleNo(cursor.getString(31 + 1));
        return pcrData;
    }
}
